from typing import Dict
from collections import namedtuple


nodes = []
nodeSets = set()
rels = []
c = []

NodeType = namedtuple("NodeType", ["name", "des", "symbolSize", "category"])
RelType = namedtuple("RelType", ["source", "target", "des"])
def CheckKey(k:str, d:Dict[str,str]):
    if k not in d:
        raise Exception(f" KeyNotFound: {k} not in {d}")
def CheckNodeExist(n:str):
    global nodeSets
    if n not in nodeSets:
        raise Exception(f"NodeNotExist: {n} not in {nodeSets}")
def DefNode(n:Dict[str,str]|NodeType):
    global nodes
    if isinstance(n, NodeType):
        n = n._asdict()
    CheckKey("name", n)
    CheckKey("des", n)
    CheckKey("symbolSize", n)
    CheckKey("category", n)
    nodes.append(n)
def DefRel(r:Dict[str,str]|RelType):
    global rels
    if isinstance(r, RelType):
        r = r._asdict()
    CheckKey("source", r)
    CheckKey("target", r)
    CheckKey("des", r)
    CheckNodeExist(r["source"])
    CheckNodeExist(r["target"])
    rels.append(r)
def GenCategory():
    global c
    global nodes
    csets = set()
    idx = 0
    for n in nodes:
        if n["category"] not in csets:
            csets.add(n["category"])
            c.append(f"类别{idx}")
            idx += 1



# 只要在这个函数里面定义就好
def Data():
    
    # 定义节点
    DefNode({"name":"A","des":"A","symbolSize":100,"category":0})
    DefNode({"name":"B","des":"B","symbolSize":80,"category":1}) 
    DefNode(NodeType("C","C",60,2))
    DefNode(NodeType("D","D",60,2))
    # 定义关系
    DefRel({"source":"A","target":"B","des":"A->B"})
    DefRel({"source":"B","target":"C","des":"B->C"})
    DefRel(RelType("B","D","B->D"))
    
    
    



# 别删
def GenData():
    Data()
    GenCategory()